package com.zhehekeji.opsys.api.service.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.zhehekeji.opsys.api.resp.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.zhehekeji.opsys.api.common.util.DateFormatUtil;
import com.zhehekeji.opsys.api.common.util.ResultCode;
import com.zhehekeji.opsys.api.enm.ElementType;
import com.zhehekeji.opsys.api.entity.TCategory;
import com.zhehekeji.opsys.api.entity.TMapping;
import com.zhehekeji.opsys.api.entity.TProduct;
import com.zhehekeji.opsys.api.entity.TProgram;
import com.zhehekeji.opsys.api.service.CommonService;

/**
 * @author chenweiliu chenwei.liu@163.com
 * @ClassName OperationManagerService
 * @Description TODO
 * @date 2015-7-7   下午2:46:33
 */
public class OperationManagerService extends CommonService implements IOperationManagerService {

    private final static Logger logger = LoggerFactory.getLogger(OperationManagerService.class);

    @Override
    public GetProductListResp getProductList(Long cpCode, int firstIndex, int maxResult) {

        GetProductListResp resp = new GetProductListResp();
        if (cpCode == null) {
            logger.error("cpCode非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("cpCode非法！");
            return resp;
        }
        String tmpWhereJpql = null;
        if (cpCode != -1) {
            tmpWhereJpql = "o.cpCode=" + cpCode + " and o.status=1";
        } else {
            tmpWhereJpql = "o.status=1";
        }
        try {
            String tmpSql = "select count(o.id) as count from com.zhehekeji.opsys.api.entity.TProduct o where " + tmpWhereJpql;
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, null, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }

            LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
            orderBy.put("sequence", "desc");
            orderBy.put("createTime", "desc");
            List<TProduct> tmpListTProduct = entityDao.getScrollData(TProduct.class, firstIndex, maxResult, tmpWhereJpql,
                    null, orderBy);
            if (tmpListTProduct == null || tmpListTProduct.isEmpty()) {
                return resp;
            }
            List<GetProductListResp.ProductInfo> tmpListProductInfo = new ArrayList<GetProductListResp.ProductInfo>();
            GetProductListResp.ProductInfo tmpProductInfo;
            for (TProduct tmpTProduct : tmpListTProduct) {
                tmpProductInfo = new GetProductListResp.ProductInfo();
                BeanUtils.copyProperties(tmpTProduct, tmpProductInfo, new String[]{"createTime"});
                tmpProductInfo.setCreateTime(DateFormatUtil.format(tmpTProduct.getCreateTime(), "yyyyMMddHHmmss"));
                tmpListProductInfo.add(tmpProductInfo);
            }
            resp.setProductInfos(tmpListProductInfo);
            resp.setRecordSize(tmpIntCount);
            logger.info("获取产品列表成功");
        } catch (Exception e) {
            logger.error("获取产品列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取产品列表失败，程序异常！");
        }
        return resp;

    }

    @Override
    public GetProductDetailResp getProductDetail(String productCode) {

        GetProductDetailResp resp = new GetProductDetailResp();
        if (StringUtils.isEmpty(productCode)) {
            logger.error("产品编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("产品编码非法！");
            return resp;
        }
        try {
            TProduct tmpTProduct = entityDao.get(TProduct.class, "o.code=?1 and o.status=1", new Object[]{productCode});
            if (tmpTProduct == null) {
                logger.error("产品（" + productCode + "）不存在或者已停用！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("产品（" + productCode + "）不存在或者已停用！");
                return resp;
            }
            BeanUtils.copyProperties(tmpTProduct, resp, new String[]{"createTime"});
            resp.setCreateTime(DateFormatUtil.format(tmpTProduct.getCreateTime(), "yyyyMMddHHmmss"));
            logger.info("获取产品详情成功");
        } catch (Exception e) {
            logger.error("获取产品详情失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取产品详情失败，程序异常！");
        }
        return resp;

    }

    @Override
    public GetCategoryDetailResp getCategoryDetail(String categoryCode) {

        GetCategoryDetailResp resp = new GetCategoryDetailResp();
        if (StringUtils.isEmpty(categoryCode)) {
            logger.error("栏目编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("栏目编码非法！");
            return resp;
        }
        try {
            TCategory tmpTCategory = entityDao.get(TCategory.class, "o.code=?1 and o.status=1", new Object[]{categoryCode});
            if (tmpTCategory == null) {
                logger.error("栏目（" + categoryCode + "）不存在或者已停用！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("栏目（" + categoryCode + "）不存在或者已停用！");
                return resp;
            }
            BeanUtils.copyProperties(tmpTCategory, resp, new String[]{"createTime"});
            resp.setCreateTime(DateFormatUtil.format(tmpTCategory.getCreateTime(), "yyyyMMddHHmmss"));
            logger.info("获取栏目详情成功");
        } catch (Exception e) {
            logger.error("获取栏目详情失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取栏目详情失败，程序异常！");
        }
        return resp;

    }

    @Override
    public GetCategoryListResp getCategoryList(Long cpCode, String categoryCode, int firstIndex, int maxResult) {

        GetCategoryListResp resp = new GetCategoryListResp();
        if (StringUtils.isEmpty(categoryCode)) {
            logger.error("栏目编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("栏目编码非法！");
            return resp;
        }
        try {
            String tmpSql = "select count(o.id) as count from TCategory o where o.parentCode=? and o.status=1";
            String tmpJpqlWhere = "o.parentCode=?1 and o.status=1";
            if (cpCode != null && cpCode != -1) {
                tmpSql = tmpSql + " and o.cpCode=" + cpCode;
                tmpJpqlWhere = tmpJpqlWhere + " and o.cpCode=" + cpCode;
            }
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{categoryCode}, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }

            LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
            orderBy.put("sequence", "desc");
            orderBy.put("createTime", "desc");
            List<TCategory> tmpListTCategory = entityDao.getScrollData(TCategory.class, firstIndex, maxResult, tmpJpqlWhere,
                    new Object[]{categoryCode}, orderBy);
            if (tmpListTCategory == null || tmpListTCategory.isEmpty()) {
                return resp;
            }
            List<GetCategoryListResp.CategoryInfo> tmpListCategoryInfo = new ArrayList<GetCategoryListResp.CategoryInfo>();
            GetCategoryListResp.CategoryInfo tmpCategoryInfo;
            for (TCategory tmpTCategory : tmpListTCategory) {
                tmpCategoryInfo = new GetCategoryListResp.CategoryInfo();
                BeanUtils.copyProperties(tmpTCategory, tmpCategoryInfo, new String[]{"createTime"});
                tmpCategoryInfo.setCreateTime(DateFormatUtil.format(tmpTCategory.getCreateTime(), "yyyyMMddHHmmss"));
                tmpListCategoryInfo.add(tmpCategoryInfo);
            }
            resp.setCategoryInfos(tmpListCategoryInfo);
            resp.setRecordSize(tmpIntCount);
            logger.info("获取栏目列表成功");
        } catch (Exception e) {
            logger.error("获取栏目列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取栏目列表失败，程序异常！");
        }
        return resp;

    }

    @Override
    public GetContentListResp getContentList(String categoryCode, String tag, String order, int firstIndex, int maxResult) {

        GetContentListResp resp = new GetContentListResp();
        if (StringUtils.isEmpty(categoryCode)) {
            logger.error("栏目编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("栏目编码非法！");
            return resp;
        }
        try {
            String tmpSql = "select count(o.id) as count from TMapping o,TProgram p where o.parentCode=? and o.status=1 and o.elementCode=p.code and p.status=1";
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{categoryCode}, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }

            String tmpProductCode = getProductCode(categoryCode);
            TProduct tmpTProduct = entityDao.get(TProduct.class, "o.code=?1", new Object[]{tmpProductCode});

            String tmpObjectSql = "select p as p from TMapping o,TProgram p where o.parentCode=? and o.status=1 and o.elementCode=p.code "
                    + "and p.status=1 order by o.sequence desc,o.createTime desc";
            List<Map<String, Object>> tmpListMapObject = entityDao.getScrollDataByHql(tmpObjectSql, new Object[]{categoryCode}, firstIndex, maxResult);
            if (tmpListMapObject == null || tmpListMapObject.isEmpty()) {
                return resp;
            }

            List<GetContentListResp.ContentInfo> tmpListContentInfo = new ArrayList<GetContentListResp.ContentInfo>();
            GetContentListResp.ContentInfo tmpContentInfo;
            TProgram tmpTProgram;
            String tmpElementType;
            for (Map<String, Object> tmpItem : tmpListMapObject) {
                tmpTProgram = (TProgram) tmpItem.get("p");
                if (tmpTProgram == null) {
                    continue;
                }
                tmpContentInfo = new GetContentListResp.ContentInfo();
                tmpElementType = tmpTProgram.getElementType();
                if (StringUtils.isEmpty(tmpElementType)) {
                    logger.error("数据错误，内容元素类型为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，内容元素类型为空！");
                    return resp;
                }
                BeanUtils.copyProperties(tmpTProgram, tmpContentInfo,
                        new String[]{"createTime", "copyrightStartTime", "copyrightEndTime"});

                tmpContentInfo.setCreateTime(DateFormatUtil.format(tmpTProgram.getCreateTime(), "yyyyMMddHHmmss"));
                if (tmpTProgram.getCopyrightEndTime() != null) {
                    tmpContentInfo.setCopyrightEndTime(DateFormatUtil.format(tmpTProgram.getCopyrightEndTime(), "yyyyMMddHHmmss"));
                }
                if (tmpTProgram.getCopyrightStartTime() != null) {
                    tmpContentInfo.setCopyrightStartTime(DateFormatUtil.format(tmpTProgram.getCopyrightStartTime(), "yyyyMMddHHmmss"));
                }
                if (tmpElementType.equals(ElementType.SERIES.getType())) {
                    tmpContentInfo.setParentName(tmpTProgram.getName());
                }
                if (tmpTProduct != null) {
                    tmpContentInfo.setProductCode(tmpProductCode);
                    tmpContentInfo.setProductName(tmpTProduct.getName());
                }

                tmpListContentInfo.add(tmpContentInfo);
            }
            resp.setContentInfos(tmpListContentInfo);
            resp.setRecordSize(tmpIntCount);
            logger.info("获取内容列表成功");
        } catch (Exception e) {
            logger.error("获取内容列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取内容列表失败，程序异常！");
        }
        return resp;

    }

    public GetContentDetailResp getContentDetail(String contentCode, String productCode) {

        GetContentDetailResp resp = new GetContentDetailResp();
        if (StringUtils.isEmpty(contentCode)) {
            logger.error("内容编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("内容编码非法！");
            return resp;
        }
        try {
            TProgram tmpTProgram = entityDao.get(TProgram.class, "o.code=?1 and o.status=1", new Object[]{contentCode});
            if (tmpTProgram == null) {
                logger.error("内容（" + contentCode + "）不存在！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("内容（" + contentCode + "）不存在！");
                return resp;
            }
            TProduct tmpTProduct = entityDao.get(TProduct.class, "o.code=?1 and o.status=1", new Object[]{productCode});
            String tmpElementType = tmpTProgram.getElementType();
            if (StringUtils.isEmpty(tmpElementType)) {
                logger.error("数据错误，内容元素类型为空！");
                resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                resp.setResultDesc("数据错误，内容元素类型为空！");
                return resp;
            }
            /**
             * 子集需要获取剧头名称
             */
            TProgram tmpParentTProgram = null;
            if (tmpElementType.equals(ElementType.CSERIES.getType())) {
                String tmpParentCode = tmpTProgram.getParentCode();
                //栏目内容编排里面绑定的是剧头
                contentCode = tmpParentCode;
                if (StringUtils.isEmpty(tmpParentCode)) {
                    logger.error("数据错误，连续剧子集剧头编码为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，连续剧子集剧头编码为空！");
                    return resp;
                }
                tmpParentTProgram = entityDao.get(TProgram.class, "o.code=?1 and o.status=1", new Object[]{tmpParentCode});
                if (tmpParentTProgram == null) {
                    logger.error("数据错误，连续剧子集剧头为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，连续剧子集剧头为空！");
                    return resp;
                }

            }

            /**
             * 获取产品编码与名称
             * 根栏目的parentCode及产品编码
             */
            List<TMapping> tmpListTMapping = entityDao.getScrollData(TMapping.class, "o.elementCode=?1 and o.status=1", new Object[]{contentCode});
            if (tmpListTMapping == null || tmpListTMapping.isEmpty()) {
                logger.error("数据错误，内容未绑定栏目！");
                resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                resp.setResultDesc("数据错误，内容未绑定栏目！");
                return resp;
            }
            if (tmpParentTProgram != null) {
                resp.setParentName(tmpParentTProgram.getName());
            }
            resp.setProductCode(productCode);
            if (tmpTProduct != null) {
                resp.setProductName(tmpTProduct.getName());
            }
            BeanUtils.copyProperties(tmpTProgram, resp,
                    new String[]{"createTime", "copyrightStartTime", "copyrightEndTime"});

            resp.setCreateTime(DateFormatUtil.format(tmpTProgram.getCreateTime(), "yyyyMMddHHmmss"));
            if (tmpTProgram.getCopyrightEndTime() != null) {
                resp.setCopyrightEndTime(DateFormatUtil.format(tmpTProgram.getCopyrightEndTime(), "yyyyMMddHHmmss"));
            }
            if (tmpTProgram.getCopyrightStartTime() != null) {
                resp.setCopyrightStartTime(DateFormatUtil.format(tmpTProgram.getCopyrightStartTime(), "yyyyMMddHHmmss"));
            }
            resp.setCreateTime(DateFormatUtil.format(tmpTProgram.getCreateTime(), "yyyyMMddHHmmss"));
            logger.info("获取内容详情成功");
        } catch (Exception e) {
            logger.error("获取内容详情失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取内容详情失败，程序异常！");
        }
        return resp;

    }

    private String getProductCode(String parentCategoryCode) throws SQLException {
        TCategory tmpParentCategory = entityDao.get(TCategory.class, "o.code=?1", new Object[]{parentCategoryCode});
        if (tmpParentCategory == null) {
            return parentCategoryCode;
        }
        return getProductCode(tmpParentCategory.getParentCode());
    }

    public GetContentListResp getSeriesSubsetList(String seriesCode, int firstIndex, int maxResult) {

        GetContentListResp resp = new GetContentListResp();
        if (StringUtils.isEmpty(seriesCode)) {
            logger.error("剧头编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("剧头编码非法！");
            return resp;
        }
        try {
            TProgram tprogram = entityDao.get(TProgram.class, "o.code=?1 and o.elementType='Series'", new Object[]{seriesCode});
            if (tprogram == null) {
                logger.error("连续剧（" + seriesCode + "）不存在！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("连续剧不存在！");
                return resp;
            }

            /**
             * 获取产品编码与名称
             * 根栏目的parentCode及产品编码
             */
            List<TMapping> tmpListTMapping = entityDao.getScrollData(TMapping.class, "o.elementCode=?1 and o.status=1", new Object[]{seriesCode});
            if (tmpListTMapping == null || tmpListTMapping.isEmpty()) {
                logger.error("数据错误，栏目内容编排未上线或者内容未绑定栏目！");
                resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                resp.setResultDesc("数据错误，栏目内容编排未上线或者内容未绑定栏目！！");
                return resp;
            }
            String tmpProductCode;
            String tmpProductCodes = "";
            String tmpProductNames = "";
            TProduct tmpTProduct;
            for (TMapping tmpTMapping : tmpListTMapping) {
                tmpProductCode = getProductCode(tmpTMapping.getParentCode());
                if (StringUtils.isEmpty(tmpProductCode)) {
                    continue;
                }
                tmpTProduct = entityDao.get(TProduct.class, "o.code=?1", new Object[]{tmpProductCode});
                if (tmpTProduct == null) {
                    continue;
                }
                if (!("," + tmpProductCodes).contains("," + tmpProductCode + ",")) {
                    tmpProductCodes = tmpProductCodes + tmpProductCode + ",";
                    tmpProductNames = tmpProductNames + tmpTProduct.getName() + ",";
                }
            }
            if (!StringUtils.isEmpty(tmpProductCodes)) {

                tmpProductCodes = StringUtils.removeEnd(tmpProductCodes, ",");
                tmpProductNames = StringUtils.removeEnd(tmpProductNames, ",");
            }
            String tmpSql = "select count(o.id) as count from TProgram o where o.parentCode=? and o.status=1";
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{seriesCode}, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }

            LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
            orderBy.put("setNumber", "asc");
            orderBy.put("createTime", "desc");
            List<TProgram> tmpListTProgram = entityDao.getScrollData(TProgram.class, firstIndex, maxResult, "o.parentCode=?1 and o.status=1",
                    new Object[]{seriesCode}, orderBy);
            if (tmpListTProgram == null || tmpListTProgram.isEmpty()) {
                return resp;
            }
            List<GetContentListResp.ContentInfo> tmpListContentInfo = new ArrayList<GetContentListResp.ContentInfo>();
            GetContentListResp.ContentInfo tmpContentInfo;
            for (TProgram tmpTProgram : tmpListTProgram) {

                tmpContentInfo = new GetContentListResp.ContentInfo();
                BeanUtils.copyProperties(tmpTProgram, tmpContentInfo,
                        new String[]{"createTime", "copyrightStartTime", "copyrightEndTime"});
                tmpContentInfo.setCreateTime(DateFormatUtil.format(tmpTProgram.getCreateTime(), "yyyyMMddHHmmss"));
                if (tmpTProgram.getCopyrightEndTime() != null) {
                    tmpContentInfo.setCopyrightEndTime(DateFormatUtil.format(tmpTProgram.getCopyrightEndTime(), "yyyyMMddHHmmss"));
                }
                if (tmpTProgram.getCopyrightStartTime() != null) {
                    tmpContentInfo.setCopyrightStartTime(DateFormatUtil.format(tmpTProgram.getCopyrightStartTime(), "yyyyMMddHHmmss"));
                }
                tmpContentInfo.setParentName(tprogram.getName());
                if (!StringUtils.isEmpty(tmpProductCodes)) {
                    tmpContentInfo.setProductCode(tmpProductCodes);
                    tmpContentInfo.setProductName(tmpProductNames);
                }
                tmpListContentInfo.add(tmpContentInfo);
            }
            resp.setContentInfos(tmpListContentInfo);
            resp.setRecordSize(tmpIntCount);
            logger.info("获取连续剧子集列表成功");
        } catch (Exception e) {
            logger.error("获取连续剧子集列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取连续剧子集列表失败，程序异常！");
        }
        return resp;

    }

    public GetContentListResp getRecommendList(String contentCode, String productCode, int firstIndex, int maxResult) {

        GetContentListResp resp = new GetContentListResp();
        if (StringUtils.isEmpty(contentCode)) {
            logger.error("内容编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("内容编码非法！");
            return resp;
        }
        if (StringUtils.isEmpty(productCode)) {
            logger.error("产品编码非法！");
            resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
            resp.setResultDesc("产品编码非法！");
            return resp;
        }
        try {
            TProgram tmpTProgram = entityDao.get(TProgram.class, "o.code=?1", new Object[]{contentCode});
            if (tmpTProgram == null) {
                logger.error("内容（" + contentCode + "）不存在！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("内容不存在！");
                return resp;
            }
            TProduct tmpTProduct = entityDao.get(TProduct.class, "o.code=?1", new Object[]{productCode});
            if (tmpTProduct == null) {
                logger.error("产品（" + productCode + "）不存在或者已停用！");
                resp.setResultCode(ResultCode.PARAMETER_IS_NOT_LEGAL);
                resp.setResultDesc("产品（" + productCode + "）不存在或者已停用！");
                return resp;
            }
            StringBuilder categoryCodeStrBuilder = new StringBuilder("'" + productCode + "',");
            getCategoryCods(productCode, categoryCodeStrBuilder);
            String tmpCategoryCodes = categoryCodeStrBuilder.substring(0, categoryCodeStrBuilder.length() - 1);

            String tmpProgramType = tmpTProgram.getProgramType();
            String tmpDirector = tmpTProgram.getDirector();
            String tmpActor = tmpTProgram.getActor();
            String whereSql = "1=1";
            if (!StringUtils.isEmpty(tmpProgramType)) {
                whereSql = whereSql + " and o.programType like '%" + tmpProgramType + "%'";
            } else {
                if (!StringUtils.isEmpty(tmpDirector)) {
                    whereSql = whereSql + " and o.director like '%" + tmpDirector + "%'";
                } else {
                    if (!StringUtils.isEmpty(tmpActor)) {
                        whereSql = whereSql + " and o.actor like '%" + tmpActor + "%'";
                    }
                }
            }
            String tmpSql = "select count(distinct o.id) as count from TProgram o, TMapping p where " + whereSql
                    + " and o.code!=? and o.code=p.elementCode and p.status=1 and p.parentCode in (" + tmpCategoryCodes + ")";
            List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{contentCode}, -1, -1);
            int tmpIntCount = 0;
            if (tmpListMapCount != null && !tmpListMapCount.isEmpty()) {
                Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
                Long tmpLongCount = (Long) tmpMapCount.get("count");
                tmpIntCount = tmpLongCount.intValue();
            }
            if (tmpIntCount <= 0) {
                return resp;
            }

            String tmpCodeSql = "select distinct o.code as code from TProgram o,TMapping p where " + whereSql
                    + " and o.code!=? and o.code=p.elementCode and p.status=1 and p.parentCode in (" + tmpCategoryCodes + ") order by p.sequence desc,p.createTime desc";
            List<Map<String, Object>> tmpListMapCode = entityDao.getScrollDataByHql(tmpCodeSql, new Object[]{contentCode}, firstIndex, maxResult);
            if (tmpListMapCode == null || tmpListMapCode.isEmpty()) {
                return resp;
            }

            List<GetContentListResp.ContentInfo> tmpListContentInfo = new ArrayList<GetContentListResp.ContentInfo>();
            GetContentListResp.ContentInfo tmpContentInfo;
            String tmpElementType;
            TProgram tmpItem;
            String tmpProgramCode;
            for (Map<String, Object> mapObject : tmpListMapCode) {
                tmpProgramCode = (String) mapObject.get("code");
                if (StringUtils.isEmpty(tmpProgramCode)) {
                    continue;
                }
                tmpItem = entityDao.get(TProgram.class, "o.code=?1", new Object[]{tmpProgramCode});
                if (tmpItem == null) {
                    continue;
                }
                tmpElementType = tmpItem.getElementType();
                if (StringUtils.isEmpty(tmpElementType)) {
                    logger.error("数据错误，内容元素类型为空！");
                    resp.setResultCode(ResultCode.DATABASE_EXCEPTION);
                    resp.setResultDesc("数据错误，内容元素类型为空！");
                    return resp;
                }

                tmpContentInfo = new GetContentListResp.ContentInfo();
                BeanUtils.copyProperties(tmpItem, tmpContentInfo,
                        new String[]{"createTime", "copyrightStartTime", "copyrightEndTime"});
                tmpContentInfo.setCreateTime(DateFormatUtil.format(tmpItem.getCreateTime(), "yyyyMMddHHmmss"));
                if (tmpItem.getCopyrightEndTime() != null) {
                    tmpContentInfo.setCopyrightEndTime(DateFormatUtil.format(tmpItem.getCopyrightEndTime(), "yyyyMMddHHmmss"));
                }
                if (tmpItem.getCopyrightStartTime() != null) {
                    tmpContentInfo.setCopyrightStartTime(DateFormatUtil.format(tmpItem.getCopyrightStartTime(), "yyyyMMddHHmmss"));
                }
                tmpContentInfo.setProductCode(productCode);
                tmpContentInfo.setProductName(tmpTProduct.getName());

                tmpListContentInfo.add(tmpContentInfo);
            }
            resp.setContentInfos(tmpListContentInfo);
            resp.setRecordSize(tmpIntCount);
            logger.info("获取内容推荐列表成功");
        } catch (Exception e) {
            logger.error("获取内容推荐列表失败，程序异常，" + e.getMessage(), e);
            resp.setResultCode(ResultCode.APPLICATION_EXCEPTION);
            resp.setResultDesc("获取内容推荐列表失败，程序异常！");
        }
        return resp;

    }

    private void getCategoryCods(String parentCategoryCode, StringBuilder strBuilder) throws SQLException {
        List<TCategory> tmpChildrenCategory = entityDao.getScrollData(TCategory.class, "o.parentCode=?1", new Object[]{parentCategoryCode});
        if (tmpChildrenCategory == null || tmpChildrenCategory.isEmpty()) {
            return;
        }
        for (TCategory tmpChildTCategory : tmpChildrenCategory) {
            strBuilder.append("'" + tmpChildTCategory.getCode() + "',");
            getCategoryCods(tmpChildTCategory.getCode(), strBuilder);
        }
    }

    @Override
    public GetScrollmsgListResp getScrollmsgList(long cpCode, int firstIndex,
                                                 int maxResult) {
        GetScrollmsgListResp resp = new GetScrollmsgListResp();
        return resp;
    }

    @Override
    public GetScrollmsgDetailResp getScrollmsgDetail(String code) {

        GetScrollmsgDetailResp resp = new GetScrollmsgDetailResp();
        return resp;
    }

    @Override
    public GetContentListResp getSearchList(String searchWord,
                                            String productCode, int firstIndex, int maxResult) {
        GetContentListResp resp = new GetContentListResp();
        return resp;
    }

    @Override
    public GetChannelListResp getChannelList(Long cpCode, String zoneCode, int firstIndex, int maxResult) {
        return new GetChannelListResp();
    }

    @Override
    public GetChannelSchedProgramListResp getChannelSchedProgramList(String channelCode, String day) {
        return new GetChannelSchedProgramListResp();
    }
}
